// /Users/terry/Project/shop/hioshop-miniprogram-dy/pages/index/index.js

const api = require('../../config/api.js');
const util = require('../../utils/util.js');

Page({
  data: {
    catelogList:[],
    choricCate:2,
    refreshTimes:1,
    goodsList:[],
    scrollTop: 0,
    isNoMore:false
  },
  onLoad: function (options) {
      this.getIndexCatelogList()
      this.getIndexGoodList()
      
      // tt.login({
      //   force: true,
      //   success(res) {
      //     console.log(res)
      //     console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
      //   },
      //   fail(res) {
      //     cosole.log(res)
      //     console.log(`login 调用失败`);
      //   },
      // });
  },
  
  getIndexCatelogList: function () {

    let that = this;
    util.request(api.indexCatalogList, {
      count: 4,
    }).then(function (res) {
      // console.log(res.data)
      that.setData({
        catelogList : res.data
      });
    });
  },
  switchCate: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      choricCate: id,
      refreshTimes:1,
      goodsList:[],
      scrollTop: 0
    });
    console.log(this.data.scrollTop)
    this.getIndexGoodList()
  },
  scroll:function(e){
    this.setData({
      scrollTop: e.detail.scrollTop
    });
  },
  scrolltolower: function (){
    let times = this.data.refreshTimes
    
    this.setData(
      {
        refreshTimes:times
      }
    )
    this.getIndexGoodList()
    
  },
  getIndexGoodList:function(){
    let that = this;
    util.request(api.getIndexGoodList, {
      pagesize: 5,
      page: that.data.refreshTimes,
      rid: that.data.choricCate,
    }).then( (res)=> {
      if (res.data.length > 0) {
        this.setData({
          goodsList:this.data.goodsList.concat(res.data),
          isNoMore:false 
        })
          
      }else{
        this.setData({
          isNoMore:true 
        })
      }
    });
  },
  firstGetIndexGoodList:function(){
    let data = this.getIndexGoodList()
    console.log(data)
    this.setData({
      goodsList:data
    })
  }
})